一直在写Java,看语法逻辑什么的没有问题
但是刚刚看书上写出来的这个程序,居然不知道怎么在goland里面运行…
于是曲线救国,了解到打印输入的参数。
这断代码就是为了找出输入数据中的重复行
- 直接启动,不带参数,启动之后输入参数
 
1 2 3 4 5
   | 参数0: /private/var/folders/lk/p8gbq87n6rvfndk7wlk23y8r0000gn/T/___go_build_dup2_go 1 # 一行输入一个 1 # 一行输入一个 ^D #这是command + D 2	1 # 输出的结果:个数 输入值
   | 
- 在命令行启动,带
 
1 2 3 4 5 6
   | $ go run dup2.go 'hisen.txt' 参数0: /var/folders/lk/p8gbq87n6rvfndk7wlk23y8r0000gn/T/go-build525680776/b001/exe/dup2 参数1: hisen.txt #代码同级目录的文件名 2       hisen 2       hisenyuan 2       123
   | 
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
   | package main
  import ( 	"bufio" 	"fmt" 	"os" 	"strconv" )
 
 
 
 
  func main() { 	 	for idx, args := range os.Args { 		fmt.Println("参数"+strconv.Itoa(idx)+":", args) 	} 	 	counts := make(map[string]int) 	 	files := os.Args[1:] 	if len(files) == 0 { 		countLines(os.Stdin, counts) 	} else { 		 		for _, arg := range files { 			 			f, err := os.Open(arg) 			if err != nil { 				fmt.Fprintf(os.Stderr, "dup2: %v\n", err) 				continue 			} 			countLines(f, counts) 			f.Close() 		} 	}
  	 	for line, n := range counts { 		if n > 1 { 			fmt.Printf("%d\t%s\n", n, line) 		} 	} }
 
  func countLines(file *os.File, counts map[string]int) { 	 	input := bufio.NewScanner(file) 	 	for input.Scan() { 		counts[input.Text()]++ 	} }
   |